<?php
include_once 'fun.php';
include_once 'islogin.php';
$_SESSION['isadmin']=isset($_SESSION['isadmin'])?$_SESSION['isadmin']:'';
  if ($_SESSION['isadmin']!=2&&$_SESSION['isadmin']!=-1) {
      header('Refresh:1;url=shop.php');
      die("请以柜台管理员身份登录");
  }
    //var_dump($_FILES);
    $Name = trim($_POST['Name']);
    $product = isset($_POST['product'])?$_POST['product']:'';
    $stuffName =isset($_POST['stuff'])?trim($_POST['stuff']):'';
    $manager  = isset($_POST['manager'])?$_POST['manager']: array();
    $stuff = explode(',', $stuffName);
    //var_dump($stuffName);
    $id=(int)$_POST['id'];
    //var_dump($id);
    if ($stuffName!='') {
        $size=count($stuff);
    } else {
        $size=0;
    }
   // $pic='pics/default.PNG';
    if (empty($Name)) {
        header('Refresh:0;url=counter.php');
        die("柜名不能为空");
    } else {
        $con=connect();
        $sql = "select * from counter where Name like '$Name' ";
        $res=query($sql, $con);
        $a=mysqli_fetch_row($res);
        if ($res->num_rows>1||($res->num_rows==1&&$a[0]!=$id)) {
            alertMes("柜名不能相同", 'counter.php');
            exit;
        }
        $sql = "update counter set Name='{$Name}' where counterid={$id}";
        query($sql, $con);
        
        for ($i=0;$i<$size;$i++) {
            //echo $stuff[$i];
            $sql = "delete from cou_man where counterid = $id and managerid = (select managerid from manager where Name like '$stuff[$i]')";
            query($sql, $con);
        }
        for ($i=0;$i<sizeof($manager);$i++) {
            $sql = "insert into cou_man  value ($id,$manager[$i])";
            query($sql, $con);
        }
        
        echo '保存成功';
        
        header('Refresh:0;url=counter.php');
    }
